package com.ruoyi.project.system.messagebooks.controller;

import java.util.List;

import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.messagebooks.domain.Messagebooks;
import com.ruoyi.project.system.messagebooks.service.IMessagebooksService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.Ztree;

/**
 * 留言板Controller
 * 
 * @author ruoyiX
 * @date 2021-12-29
 */

@Controller
@RequestMapping("/system/messagebooks")
public class MessagebooksController extends BaseController
{
    public String hello(){
        return "Hello,world!";
    }
    private String prefix = "system/messagebooks";

    @Autowired
    private IMessagebooksService messagebooksService;

    @RequiresPermissions("system:messagebooks:view")
    @GetMapping()
    public String messagebooks()
    {
        return prefix + "/messagebooks";
    }

    /**
     * 查询留言板树列表
     */
    @RequiresPermissions("system:messagebooks:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Messagebooks> list(Messagebooks messagebooks)
    {
        List<Messagebooks> list = messagebooksService.selectMessagebooksList(messagebooks);
        return list;
    }

    /**
     * 导出留言板列表
     */
    @RequiresPermissions("system:messagebooks:export")
    @Log(title = "留言板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Messagebooks messagebooks)
    {
        List<Messagebooks> list = messagebooksService.selectMessagebooksList(messagebooks);
        ExcelUtil<Messagebooks> util = new ExcelUtil<Messagebooks>(Messagebooks.class);
        return util.exportExcel(list, "留言板数据");
    }

    /**
     * 新增留言板
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("messagebooks", messagebooksService.selectMessagebooksById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存留言板
     */
    @RequiresPermissions("system:messagebooks:add")
    @Log(title = "留言板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Messagebooks messagebooks)
    {
        return toAjax(messagebooksService.insertMessagebooks(messagebooks));
    }

    /**
     * 修改留言板
     */
    @RequiresPermissions("system:messagebooks:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Messagebooks messagebooks = messagebooksService.selectMessagebooksById(id);
        mmap.put("messagebooks", messagebooks);
        return prefix + "/edit";
    }

    /**
     * 修改保存留言板
     */
    @RequiresPermissions("system:messagebooks:edit")
    @Log(title = "留言板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Messagebooks messagebooks)
    {
        return toAjax(messagebooksService.updateMessagebooks(messagebooks));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:messagebooks:remove")
    @Log(title = "留言板", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(messagebooksService.deleteMessagebooksById(id));
    }

    /**
     * 选择留言板树
     */
    @GetMapping(value = { "/selectMessagebooksTree/{id}", "/selectMessagebooksTree/" })
    public String selectMessagebooksTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("messagebooks", messagebooksService.selectMessagebooksById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载留言板树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = messagebooksService.selectMessagebooksTree();
        return ztrees;
    }
}
